//
//  FaceView.m
//  JST
//
//  Created by Mac on 15/8/25.
//  Copyright (c) 2015年 aniuge. All rights reserved.
//

#import "FaceView.h"

#define lines 3
#define numberLine 7
#define FaceSize  kCurrentScreenWidth(35)
/*
 ** 两边边缘间隔
 */
#define EdgeDistance 20
/*
 ** 上下边缘间隔
 */
#define EdgeInterVal 5
@interface FaceView ()
@property (nonatomic,assign)NSInteger page;
@end
@implementation FaceView

- (id)initWithFrame:(CGRect)frame forIndexPath:(NSInteger)index
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        _page = index;
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    // 水平间隔
    CGFloat horizontalInterval = (CGRectGetWidth(self.bounds)-numberLine*FaceSize -2*EdgeDistance)/(numberLine-1);
    // 上下垂直间隔
    CGFloat verticalInterval = (CGRectGetHeight(self.bounds)-2*EdgeInterVal -lines*FaceSize)/(lines-1);
    for (int i=0; i<lines; i++) {
        for (int j=0; j<numberLine; j++) {
            UIButton *customBut=[UIButton buttonWithType:UIButtonTypeCustom];
            customBut.frame=CGRectMake(j*FaceSize+EdgeDistance+j*horizontalInterval,
                                       i*FaceSize +i*verticalInterval+EdgeInterVal,
                                       FaceSize,
                                       FaceSize);
            
            
            [self addSubview:customBut];
            if (i*numberLine+j+1==21) {
                if (isIPhone4||isIPhone5) {
                    [customBut setBackgroundImage:[UIImage imageNamed:@"healthy_comments_delete"] forState:UIControlStateNormal];
                } else {
                    [customBut setImage:[UIImage imageNamed:@"healthy_comments_delete"] forState:UIControlStateNormal];
                }
                
            } else {
            
                [customBut setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"e_%zd",_page*20+i*numberLine+j+1]] forState:UIControlStateNormal];
                customBut.tag=_page*20+i*numberLine+j+1;
            }
            //添加事件
            [customBut addTarget:self action:@selector(customButPress:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
#pragma mark ================点击事件======================
-(void)customButPress:(UIButton *)but
{
    if (_delegate&&[_delegate respondsToSelector:@selector(faceViewWithFaceButtonClick:)])
    {
        [_delegate faceViewWithFaceButtonClick:but];
    }
}
@end
